package com.ligure.service;

import com.ligure.spring.BeanPostProcessor;
import com.ligure.spring.Component;

import java.lang.reflect.Proxy;

@Component
public class MyProxyCreator implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        System.out.println(beanName + " AutoProxyCreator#postProcessBeforeInitialization");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println(beanName + " AutoProxyCreator.postProcessAfterInitialization");
        if (("myUserService").equals(beanName)) {
            Object proxyInstance = Proxy.newProxyInstance(MyProxyCreator.class.getClassLoader(), bean.getClass().getInterfaces(), (proxy, method, args) -> {
                System.out.println("AOP业务逻辑");
                return method.invoke(bean, args);
            });
            return proxyInstance;
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

}
